package b2.widget.vip
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.widget.*;
	import b2.widget.login.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;

	public class UpgradeDialog extends Sprite
	{
		private const present:Object = {0: 25, 2: 15, 4: 1}; 
		private var upgradePresents:Object;
		
		public function UpgradeDialog()
		{
			super();
			//对话框背景
			var VipUpgradeDialogBgr:Class = ResourceHelper.getInstance().getClassByName("VipUpgradeDialogBgr");
			addChild(new VipUpgradeDialogBgr());
			//关闭按钮
			var VipButtonClose1:Class = ResourceHelper.getInstance().getClassByName("VipButtonClose1");
			var VipButtonClose2:Class = ResourceHelper.getInstance().getClassByName("VipButtonClose2");
			var closeIcon:ImgSwitchIcon = new ImgSwitchIcon(VipButtonClose1, VipButtonClose2);
			closeIcon.x = 490;
			closeIcon.y = 15;
			addChild(closeIcon);
			closeIcon.addEventListener(MouseEvent.CLICK, onButtonCloseClick);
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			upgradePresents = new Object();
			var objectIndex:String;
			for (var i:int = me.currentLevel; i < me.level; i++)
			{
				for (objectIndex in present)
				{
					var objectNum:int = present[objectIndex] * (i + 1);
					if (upgradePresents[objectIndex] == null)
					{
						upgradePresents[objectIndex] = objectNum;
					}
					else
					{
						upgradePresents[objectIndex] += objectNum;
					}
				}
			}
			for (i = 0; i < 2; i++)
			{
				var presentContainer:Sprite = new Sprite();
				var presentCount:int = 0;
				//礼物
				for (objectIndex in upgradePresents)
				{
					objectNum = upgradePresents[objectIndex];
					var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
					var objectItem:ObjectItem = new ObjectItem(parseInt(objectIndex), 0, 0, -1, objectNum, -1, false);
					objectItem.width = 80;
					objectItem.scaleY = objectItem.scaleX;
					objectItem.x = presentCount * 140;
					presentContainer.addChild(objectItem);
					//标题名字
					var titleText:TextField = new TextField();
					titleText.selectable = false;
		        	titleText.autoSize = TextFieldAutoSize.LEFT;
					//装备
					if (objectConfig.type == 1)
					{
						var equipConfig:EquipConfig = objectConfig as EquipConfig;
						titleText.text = Config.basicConfig.equipSuitName[equipConfig.suitIndex] + "的";
					}
					titleText.appendText(objectConfig.name + " × " + objectNum);
					if (titleText.text.length > 6)
					{
						titleText.setTextFormat(new TextFormat("微软雅黑", 14, 0xF9F744, true));
					}
					else
					{
						titleText.setTextFormat(new TextFormat("微软雅黑", 15, 0xF9F744, true));
					}
					titleText.filters = [new GlowFilter(0x8F470E, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
					titleText.x = presentCount * 140 + 40 - titleText.width / 2;
					titleText.y = 85;
					presentContainer.addChild(titleText);
					//锁
					if (!(me.is_yellow_vip || me.is_yellow_year_vip) && i == 1)
					{
						var VipLock:Class = ResourceHelper.getInstance().getClassByName("VipLock");
						var lock:Sprite = new VipLock();
						lock.width = 40;
						lock.scaleY = lock.scaleX;
						lock.x = presentCount * 140 - 10;
						lock.y = -20;
						presentContainer.addChild(lock);
					}
					presentCount++;
				}
				presentContainer.mouseChildren = false;
				presentContainer.x = (this.width - presentContainer.width) / 2;
				presentContainer.y = 115 + i * 165;
				addChild(presentContainer);
			}
			if (me.is_yellow_vip || me.is_yellow_year_vip)
			{
				//立即领取
				var VipButtonFetch2:Class = ResourceHelper.getInstance().getClassByName("VipButtonFetch2");
				var vipFetchButton:BasicButton = new BasicButton(VipButtonFetch2);
				vipFetchButton.x = 120;
				vipFetchButton.y = 400;
				vipFetchButton.addEventListener(MouseEvent.CLICK, onButtonFectchClick);
				addChild(vipFetchButton);
				//续费黄钻
				var vipButtonResume:VipButtonResume = new VipButtonResume();
				vipButtonResume.x = 300;
				vipButtonResume.y = 400;
				addChild(vipButtonResume);
			}
			else
			{
				var VipButtonFetch:Class = ResourceHelper.getInstance().getClassByName("VipButtonFetch");
				var buttonOK:BasicButton = new BasicButton(VipButtonFetch);
				buttonOK.x = 160;
				buttonOK.y = 404;
				buttonOK.addEventListener(MouseEvent.CLICK, onButtonFectchClick);
				addChild(buttonOK);
				//开通黄钻
				var vipButtonOpen:VipButtonOpen = new VipButtonOpen();
				vipButtonOpen.x = 290;
				vipButtonOpen.y = 400;
				addChild(vipButtonOpen);
			}
		}
		
		private function onButtonFectchClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var objectBalance:String = "";
			//礼物
			var presentCount:int;
			for (var objectIndex:String in upgradePresents)
			{
				var objectNum:int = upgradePresents[objectIndex];
				if (me.is_yellow_vip || me.is_yellow_year_vip)
					objectNum *= 2;
				if (presentCount++ > 0)
					objectBalance += ",";	
				objectBalance += me.id + "," + objectIndex + ",0,0,0," + objectNum;
			}
			ServerHelper.getInstance().addEventListener(ServerEvent.FETCH_UPGRADE_GIFT, onFetchFinish);
			ServerHelper.getInstance().fetchUpgradeGift(objectBalance);
			close();
		}
		
		private function onFetchFinish(event:ServerEvent):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.FETCH_UPGRADE_GIFT, onFetchFinish);
			var giftItems:Array = new Array();
			for (var objectIndex:String in upgradePresents)
			{
				var objectNum:int = upgradePresents[objectIndex];
				var giftItem:GiftItem = new GiftItem([objectIndex+ ",0," + objectNum]);
				giftItems.push(giftItem);
			}
			var awardMessage:AwardMessage = new AwardMessage("您获得以下奖励：", "", giftItems);
			WidgetManager.getInstance().popOut([awardMessage], true, true);
		}
		
		private function onButtonCloseClick(event:MouseEvent):void
		{
			close();
		}
		
		public function close():void
        {
        	if (this.parent != null)
        	{
        		this.parent.removeChild(this);
        	}
        }
		
	}
}